package com.wzd.testimage.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by hello on 15-5-27.
 */
public class MyImageView extends ImageView {
    private Bitmap mBitmap;
    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        mBitmap = bm;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            scaleImage(event);
//            rotateImage(event);
        } else if (event.getPointerCount() == 1) {
            moveImage(event);
        }
        return true;
    }
    private float oldRotation=0;
    private Matrix mMatrix=new Matrix();
    private void rotateImage(MotionEvent event) {
        switch (event.getAction()&MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_POINTER_DOWN:
                oldRotation = rotation(event);
                break;
            case MotionEvent.ACTION_MOVE:
                this.setScaleType(ScaleType.MATRIX);
                mMatrix.postRotate(rotation(event) - oldRotation, getWidth() / 2, getHeight() / 2);
                this.setImageMatrix(mMatrix);
                oldRotation = rotation(event);
                break;
        }
    }
    // 取旋转角度
    private float rotation(MotionEvent event) {
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }
    private float downLen;
    private float moveLen;

    private void scaleImage(MotionEvent event) {
        float dx = event.getX(1) - event.getX(0);
        float dy = event.getY(1) - event.getY(0);
        switch (event.getAction()) {
            case MotionEvent.ACTION_POINTER_2_DOWN:
                downLen = (float) Math.sqrt(dx * dx + dy * dy);
                break;
            case MotionEvent.ACTION_MOVE:
                moveLen = (float) Math.sqrt(dx * dx + dy * dy);
                float dLen = moveLen - downLen;
                float scale = Math.abs((dLen / 800) * 0.5f);
                if (dLen == 0) return;
                if (dLen > 0) {
                    scale(1, scale);
                } else {
                    scale(0, scale);
                }
                downLen = moveLen;
                break;
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = canvas.getClipBounds();
        rect.left++;
        rect.top++;
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(rect, paint);
    }
    private void scale(int i, float scale) {
        if (i == 0) {
            setFrame(getLeft() + (int) (scale * getWidth()),
                    getTop() + (int) (scale * getHeight()),
                    getRight() - (int) (scale * getWidth()),
                    getBottom() - (int) (scale * getHeight()));
        } else {
            setFrame(getLeft() - (int) (scale * getWidth()),
                    getTop() - (int) (scale * getHeight()),
                    getRight() + (int) (scale * getWidth()),
                    getBottom() + (int) (scale * getHeight()));
        }
    }
    private float downX = 0;
    private float downY = 0;
    private void moveImage(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float moveY = event.getY();
                float dx = moveX - downX;
                float dy = moveY - downY;
                if(Math.abs(getScrollX())>getWidth()/2||Math.abs(getScrollY())>getHeight()/2){
                    scrollTo(0,0);
                }else {
                    scrollBy((int)-dx,(int)-dy);
                }
                downX = moveX;
                downY = moveY;
                break;

        }
    }
}
